//
//  BHSportRecordViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/15.
//

import UIKit

class BHSportRecordViewController: BHBaseViewController {

    lazy var navigationBar: BHSportRecordNavigationBar = {
        let view = BHSportRecordNavigationBar.loadFromXib()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.begainY)
        return view
    }()
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight)
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .white
        view.register(UINib.init(nibName: "BHSportRecordTableViewCell", bundle: nil), forCellReuseIdentifier: "BHSportRecordTableViewCell")

        return view
    }()
    
    fileprivate var type: Int = 0
    fileprivate var recordsArray: [BHSportRunOrOtherInfoModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.removeFromSuperview()
        self.view.addSubview(self.navigationBar)
        self.view.addSubview(self.tableView)
        
        self.navigationBar.didChangeIndexActBlock = { [weak self] index in
            if index == -1 {
                self?.navigationController?.popViewController(animated: true)
            }else {
                SVProgressHUD.show()
                self?.type = index
                self?.page = 1
                self?.queryRecordService()
            }
        }
        
        self.tableView.addRefresh { [weak self] in
            self?.page = 1
            self?.queryRecordService()
        }
        
        self.tableView.addloadMore {[weak self] in
            self?.page += 1
            self?.queryRecordService()
            
        }
        
        self.queryRecordService()
    }
    


}

extension BHSportRecordViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.recordsArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
  
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

extension BHSportRecordViewController: UITableViewDelegate {
  
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHSportRecordTableViewCell", for: indexPath) as! BHSportRecordTableViewCell
        let model = self.recordsArray[indexPath.row]
        cell.model = model
        return cell
    }
}


extension BHSportRecordViewController {
    func queryRecordService()  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.sport_runOrOther_record_url, paramters: ["pageNo":self.page,"pageSize":15,"type":self.type]) { (success, errorMsg, responseDic) in
            self.tableView.endRefresh()
            self.tableView.endLoading()
            
            SVProgressHUD.dismiss()
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                let records = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
                let total = BHJsonTool.decodeIntFromDic(dic: result, key: "total")
                if self.page == 1 {
                    self.tableView.endLoadWithNoMoreData(false)
                    if self.recordsArray.count != 0 {
                        self.recordsArray.removeAll()
                    }
                }
                
                for item in records {
                    let model = BHSportRunOrOtherInfoModel.init()
                    model.parseDic(item as! [String:Any])
                    self.recordsArray.append(model)
                }
                self.tableView.reloadData()
                
                if self.recordsArray.count >= total {
                    self.tableView.endLoadWithNoMoreData(true)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
}
